<?php

require_once 'Mysql.php';

class Resources {
function resource_get_id($title, $year, $type_id){ 
	//$mysql_r_g_i = New Mysql();
	$mysql = New Mysql();
	$stmt = $mysql->run_query("SELECT id FROM resource WHERE title=? AND year=? AND type_id=?", $title, $year, $type_id);
	$stmt->bind_result($_id);

	if($stmt->fetch()) {		
		$stmt->close();
		return $_id;
	} else {
		//Return id of 0 if it does not exist
		$stmt->close(); 
		return 0;
	}    
}

function resource_add($title, $year, $type_id){  
	$mysql = New Mysql(); 
	$mysql->run_query_string("INSERT INTO resource (title,year,type_id) VALUES('$title','$year','$type_id')");
	return $this->resource_get_id($title, $year, $type_id);
}

//for movie
function movie_check_id($rid){ 
	$mysql = New Mysql();  
	$stmt = $mysql->run_query("SELECT id FROM movie WHERE id=? LIMIT 1", $rid);
	$stmt->bind_result($rid);	
	if($stmt->fetch()){		
		$stmt->close(); 
	//	echo "<br/>mc_i:".$rid."<br/>";
		return $rid;
	} else {
		$stmt->close(); 
		//Return id of 0 if it does not exist
		return 0;
	}    
}

function movie_add($rid, $length, $rating, $imdb_link){
	$mysql = New Mysql();
	$mysql->run_query_string("INSERT INTO movie (id,length,rating,imdb_link) VALUES('$rid','$length','$rating','$imdb_link')");
	return $this->movie_check_id($rid);
}

function add_movie($title, $year, $length, $rating, $imdb_link) {
	$movie_type = 1;
	$rid = $this->resource_get_id($title, $year, $movie_type);
	//echo "<br/>"."add movie_r_getid:".$rid."<br/>";
	if($rid == 0) {
		//ADD resource
		$rid = $this->resource_add($title, $year, $movie_type);
		//echo "<br/>"."add movie_r_add:".$rid."<br/>";
		if($rid != 0){
			$rid = $this->movie_add($rid, $length, $rating, $imdb_link);
			//echo "<br/>"."add movie_movie_add:".$rid."<br/>";
			if($rid != 0){
				return "Movie Added";
			} else {
				return "Failed to Add Movie";
			}
			return "Resource Added";
		} else {
			return "FAILED to add Resource";
		}
	} 
	return "Not Added Already Exists";
	//header("location: view_resource.php?rid=$movie_rid");	
} 


//for game
function game_check_id($rid){ 
	$mysql = New Mysql();  
	$stmt = $mysql->run_query("SELECT id FROM videogame WHERE id=? LIMIT 1", $rid);
	$stmt->bind_result($rid);	
	if($stmt->fetch()){		
		$stmt->close(); 
		//echo "<br/>mc_i:".$rid."<br/>";
		return $rid;
	} else {
		$stmt->close(); 
		//Return id of 0 if it does not exist
		return 0;
	}    
}

function game_add($rid, $rating, $company, $console){
	$mysql = New Mysql();
	$mysql->run_query_string("INSERT INTO videogame (id,rating,company,console) VALUES('$rid','$rating','$company','$console')");
	return $this->game_check_id($rid);
}

function add_game($title, $year, $rating, $company, $console) {
	$game_type = 4;
	$rid = $this->resource_get_id($title, $year, $game_type);
	//echo "<br/>"."add movie_r_getid:".$rid."<br/>";
	if($rid == 0) {
		//ADD resource
		$rid = $this->resource_add($title, $year, $game_type);
		//echo "<br/>"."add movie_r_add:".$rid."<br/>";
		if($rid != 0){
			$rid = $this->game_add($rid, $rating, $company, $console);
			//echo "<br/>"."add movie_movie_add:".$rid."<br/>";
			if($rid != 0){
				return "Game Added";
			} else {
				return "Failed to Add Game";
			}
			return "Resource Added";
		} else {
			return "FAILED to add Resource";
		}
	} 
	return "Not Added Already Exists";
	//header("location: view_resource.php?rid=$movie_rid");	
}


//for book
function book_check_id($rid){ 
	$mysql = New Mysql();  
	$stmt = $mysql->run_query("SELECT id FROM book WHERE id=? LIMIT 1", $rid);
	$stmt->bind_result($rid);	
	if($stmt->fetch()){		
		$stmt->close(); 
		//echo "<br/>mc_i:".$rid."<br/>";
		return $rid;
	} else {
		$stmt->close(); 
		//Return id of 0 if it does not exist
		return 0;
	}    
}

function book_add($rid, $publisher, $ISBN){
	$mysql = New Mysql();
	$mysql->run_query_string("INSERT INTO book (id,publisher,isbn) VALUES('$rid','$publisher','$ISBN')");
	return $this->book_check_id($rid);
}

function add_book($title, $year, $publisher, $ISBN) {
	$book_type = 2;
	$rid = $this->resource_get_id($title, $year, $book_type);
	//echo "<br/>"."add movie_r_getid:".$rid."<br/>";
	if($rid == 0) {
		//ADD resource
		$rid = $this->resource_add($title, $year, $book_type);
		//echo "<br/>"."add movie_r_add:".$rid."<br/>";
		if($rid != 0){
			$rid = $this->book_add($rid, $publisher, $ISBN);
			//echo "<br/>"."add movie_movie_add:".$rid."<br/>";
			if($rid != 0){
				return "Book Added";
			} else {
				return "Failed to Add Book";
			}
			return "Resource Added";
		} else {
			return "FAILED to add Resource";
		}
	} 
	return "Not Added Already Exists";
	//header("location: view_resource.php?rid=$movie_rid");	
} 

//for album
function audio_check_id($rid){ 
	$mysql = New Mysql();  
	$stmt = $mysql->run_query("SELECT id FROM album WHERE id=? LIMIT 1", $rid);
	$stmt->bind_result($rid);	
	if($stmt->fetch()){		
		$stmt->close(); 
		//echo "<br/>mc_i:".$rid."<br/>";
		return $rid;
	} else {
		$stmt->close(); 
		//Return id of 0 if it does not exist
		return 0;
	}    
}

function audio_add($rid, $track){
	$mysql = New Mysql();
	$mysql->run_query_string("INSERT INTO album (id,no_of_tracks) VALUES('$rid','$track')");
	return $this->audio_check_id($rid);
}

function add_audio($title, $year, $track) {
	$audio_type = 3;
	$rid = $this->resource_get_id($title, $year, $audio_type);
	//echo "<br/>"."add movie_r_getid:".$rid."<br/>";
	if($rid == 0) {
		//ADD resource
		$rid = $this->resource_add($title, $year, $audio_type);
		//echo "<br/>"."add movie_r_add:".$rid."<br/>";
		if($rid != 0){
			$rid = $this->audio_add($rid, $track);
			//echo "<br/>"."add movie_movie_add:".$rid."<br/>";
			if($rid != 0){
				return "Album Added";
			} else {
				return "Failed to Add Album";
			}
			return "Resource Added";
		} else {
			return "FAILED to add Resource";
		}
	} 
	return "Not Added Already Exists";
	//header("location: view_resource.php?rid=$movie_rid");	
} 


function add_resource_copy($rid, $uid, $condition, $share_lvl, $format_id){	 
	$mysql = New Mysql(); 
	$mysql->run_query_string("INSERT INTO resource_copy (resource_id,u_id,condition,share_level,format_id) VALUES('$rid','$uid','$condition','$share_levl','$format_id')");
	return $this->resource_copy_get_id($rid, $uid, $format);
}
	//header("location: view_resource_copy.php?rcid=$rcid");   
}
